const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const CourseUser = sequelize.define('CourseUser', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  course_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '课程ID'
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: '用户ID'
  },
  order_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: '订单ID'
  },
  progress: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: '学习进度(百分比)'
  },
  status: {
    type: DataTypes.ENUM('active', 'inactive'),
    defaultValue: 'active',
    comment: '状态'
  },
  last_learn_time: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: '最后学习时间'
  },
  created_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '创建时间'
  },
  updated_at: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW,
    comment: '更新时间'
  }
}, {
  tableName: 'course_users',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    {
      fields: ['course_id']
    },
    {
      fields: ['user_id']
    },
    {
      fields: ['course_id', 'user_id'],
      unique: true
    }
  ]
});

module.exports = CourseUser; 